import requests
import time
import json


def scan_pay_api(order_id, pay_type, phone, amount, sjamount, user_url):
    try:
        url = 'http://106.52.153.105:81/api/pf/pay'
        if pay_type == 0:
            pay_type_str = 'alipay'
        else:
            pay_type_str = 'wxpay'
        params = {
            'order_id': 'HM{}{}'.format(pay_type_str, order_id),
            'amount': '{}'.format(amount),
            'time': str(int(time.time())),
            'username': phone,
            'sjamount': '{}'.format(sjamount),
            'key': '69e7b46371e0222d135abd788003e1b2',
            'xmf': 'HM',
            'url': user_url,
            'paytype': pay_type_str,
            'notifyurl': 'http://123.57.26.91:5000/api/v2.0/scan_notify/',
            'backurl': 'http://123.57.26.91:5000/api/v2.0/scan_back/',

        }

        result = requests.post(url=url, params=params)
        result_dict = json.loads(result.content)

        if result_dict['status'] == 1000:
            return True
        else:
            # print(result.text.encode('utf-8').decode('unicode_escape'))
            print(result_dict)
            return False
    except Exception as e:
        print('FOSSpay发生错误', e)
        return False


# foss_pay_api('1', 1, '13333333333', 1, 0.01, 'wxp://f2f0UcQEkak5YfnP_6-5AhGApKKw5entzCOz')


